package com.zhiwang.planet.ui.guessboard

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerGameBaseComponent
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.di.module.GameModule
import com.zhiwang.planet.mvp.p.RankPresenter
import com.zhiwang.planet.mvp.v.RankDataView
import com.zhiwang.planet.mvp.v.RankProfitClaimView
import com.zhiwang.planet.mvvm.model.entity.RankModel
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.StatusbarUtil
import com.zhiwang.planet.util.Utils
import kotlinx.android.synthetic.main.activity_guess_list.*
import javax.inject.Inject

/**
 * 竞猜榜单
 */
class RankActivity : BaseActivity(), RankDataView, RankProfitClaimView {

    val adapter = RankAdapter()
    var isYesterday = false

    companion object {
        fun startForYesterday(context: Context) {
            val intent = Intent(context, RankActivity::class.java)
            intent.putExtra("YesterdayKey", true)
            context.startActivity(intent)
        }
    }

    @Inject
    lateinit var rankPresenter: RankPresenter


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guess_list)
        setSupportActionBar(toolbar)
        com.zhiwang.planet.util.Utils.setStatusBar(this, false, false)
        StatusbarUtil.setStatusBarColor(this, R.color.color_guess_rank_primary)
        init()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        if (isYesterday.not())
            menuInflater.inflate(R.menu.guess_rank_menu, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item!!.itemId == R.id.yesterday_rank_board) {
            startForYesterday(this)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun initInjection() {
        isYesterday = intent.getBooleanExtra("YesterdayKey", false)
        DaggerGameBaseComponent.builder()
                .appComponent(getAppComponent())
                .autoDisposeModule(AutoDisposeModule(this))
                .gameModule(GameModule(isYesterday))
                .build()
                .inject(this)
    }

    private fun init() {
        if (isYesterday) {
            today_bean_text.text = "昨日盈利趣豆："
            today_rank_text.text = "昨日排名："
            toolbar.title = "昨日竞猜排行榜"
        } else {
            rank_btn.visibility = View.GONE
        }
        recyclerView.adapter = adapter
        smartRefresh.setOnRefreshListener {
            rankPresenter.getRankList()
        }
        rankPresenter.rankView = this
        rankPresenter.rankProfitClaimView = this
        rankPresenter.getRankList()
    }

    @SuppressLint("SetTextI18n")
    override fun onLoadRankData(rankModel: RankModel) {
        adapter.setData(rankModel.rankList)
        today_bean_num.text = rankModel.rankProfit
        today_rank.text = if (rankModel.rank == "0") "未进入排名" else "第${rankModel.rank}名"
        rank_btn.isEnabled = rankModel.rankStatus == 1
        smartRefresh.finishLoadMore()
        smartRefresh.finishRefresh()
    }

    /**
     * 领取竞猜奖励
     */
    fun claimRankProfit(v: View) {
        rankPresenter.claimRankProfit()
    }

    override fun onRankProfitClaimed() {
        rank_btn.isEnabled = false
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }
}
